#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

set -e

REPO_PATH="$(dirname "$(dirname "$(realpath "$0")")")"

# Detect whether or not mypy is available and print out an error that is in the
# same format as mypy issues. This message is understood by e.g. vims ALE and
# displays and error that mypy could not be executed.
VENV_MYPY_PATH="$REPO_PATH/.venv/bin/mypy"
if [ ! -f "$VENV_MYPY_PATH" ]; then
    # Try to fix this first
    # the .venv folder it self can't be removed as it is a mount point in docker
    rm -rf "$REPO_PATH/.venv/**/*"
    make -C "$REPO_PATH" .venv 1>&2

    if [ ! -f "$VENV_MYPY_PATH" ]; then
        for LAST_ARG; do :; done
        echo "$LAST_ARG:1: error: Could not find $VENV_MYPY_PATH"
        exit 1
    fi
fi

# shellcheck disable=SC2086
exec "$REPO_PATH/scripts/run-uvenv" mypy ${JUNIT_XML} "$@"
